function noecho() {
        str="$1"
        if [ `echo "\\c" | wc -l` -eq 0 ]; then
                fo="";bo="\c"
        else
                fo="-n";bo=""
        fi

        echo $fo "$str" $bo
}

function xverify() {
        qstr=$1  # query string
        yes=$2  # affirmative choice
        no=$3   # negative choice
        def=$4  # default choice

        while true; do \
                # issue query
                noecho "$qstr? ($yes/$no): "
                read ans < /dev/tty
                if [ -z "$ans" ]; then
                        ans=$def
                fi

                # translate all to lowercase.
                tyes=`echo $yes | tr [:upper:] [:lower:]`
                tno=`echo $no |  tr [:upper:] [:lower:]`
                ans=`echo $ans |  tr [:upper:] [:lower:]`

                if [ "$ans" = "$tyes" ]; then
                        return 0
                elif [ "$ans" = "$tno" ]; then
                        return 1
                else
                        echo ""
                        echo "Please enter '$yes' for Yes or '$no' for No."
                        echo "Lower or Upper case letters may be used."
                        echo "The capital letter is the default."
                fi
        done
}

function usage() {
	echo "install.sh [prod]"
	echo "Defaults to pre production if any arg other than prod is used."
	exit 0
}

case $1 in
	-help|--help|-h|--h) usage
        ;;

        'prod') xverify "Production mode install, continue" y N N || exit 0
		echo "Entering production installation."
		topLevelConf=prod_conf.properties
	        topLevelInstallConf=prod_install.conf
		appLevelConf=service_prod_conf.properties
	   	oracle_src_cnf=prod_oracle_conf.sh
	   	oracle_src_env=prod_oracle_env.sh
		hiveConf=prod_hive.conf
        ;;

	'vm') echo "WillOS-VM mode install."
	   topLevelConf=vm_conf.properties
	   topLevelInstallConf=vm_install.conf
	   appLevelConf=service_vm_conf.properties
	   oracle_src_cnf=vm_oracle_conf.sh
	   oracle_src_env=vm_oracle_env.sh
	   hiveConf=vm_prod_hive.conf
        ;;

        *) echo "Pre-Production mode install."
	   topLevelConf=preprod_conf.properties
	   topLevelInstallConf=preprod_install.conf
	   appLevelConf=service_preprod_conf.properties
	   oracle_src_cnf=preprod_oracle_conf.sh
	   oracle_src_env=preprod_oracle_env.sh
	   hiveConf=preprod_hive.conf
        ;;
esac

if [ ! -f "${AGG_CONF_DIR}/${topLevelConf}" ]; then
        echo "Configuration file ${AGG_CONF_DIR}/${topLevelConf} does not exist. Exiting."
        exit
else
        cp "${AGG_CONF_DIR}/${topLevelConf}" "$AGG_CONF_DIR/conf.properties"
fi

# Optional top level hive config.
if [ -f "${AGG_CONF_DIR}/${hiveConf}" ]; then
        cp "${AGG_CONF_DIR}/${hiveConf}" "$AGG_CONF_DIR/hive.conf"
	chmod 755 "$AGG_CONF_DIR/hive.conf"
fi

if [ ! -f "${AGG_WORKFLOW_CONF_DIR}/${appLevelConf}" ]; then
        echo "Configuration file ${AGG_WORKFLOW_CONF_DIR}/${appLevelConf} does not exit. Exiting."
        exit
else
        cp ${AGG_WORKFLOW_CONF_DIR}/${appLevelConf} ${AGG_WORKFLOW_CONF_DIR}/conf.properties
        source ${AGG_WORKFLOW_CONF_DIR}/conf.properties
fi

# Get common installation globals, if present.
if [ -f "$AGG_CONF_DIR/${topLevelInstallConf}" ]; then
        source $AGG_CONF_DIR/${topLevelInstallConf}
fi

# Replace Oracle config files
OracleTargetDir="$AGG_WORKING_DIR/oracle/bin"
OracleSourceCnf="$AGG_WORKING_DIR/conf/${oracle_src_cnf}"
OracleSourceEnv="$AGG_WORKING_DIR/conf/${oracle_src_env}"
if [ ! -d "$AGG_WORKING_DIR/oracle/bin" ]; then
	echo "Oracle directory missing! -- exiting."
	exit 1
fi
cp -f $OracleSourceCnf "$OracleTargetDir/oracle_conf.sh"
cp -f $OracleSourceEnv "$OracleTargetDir/oracle_env.sh"
